/**
 * @author wxn
 * @since 12-5-24 1:24
 */
package com.sihai.ehr.web.action.organization;

import com.sihai.core.LimitedList;
import com.sihai.core.beans.BeanInject;
import com.sihai.ehr.jdbc.Department;
import com.sihai.ehr.jdbc.DepartmentService;
import com.sihai.ehr.model.sys.AuditStatus;
import com.sihai.ehr.model.sys.Organization;
import com.sihai.ehr.service.OrganizationService;
import com.sihai.ehr.web.action.EHRAction;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;

import java.util.ArrayList;
import java.util.List;

@Results({@Result(name = "apply_list", location = "apply_list.jsp"),
        @Result(name = "apply_index", location = "apply_index.jsp"),
        @Result(name = "apply", location = "apply.jsp")})
public class ApplyAction extends EHRAction {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 7204414972672240877L;

    @BeanInject
    private OrganizationService organizationService;

    private Organization organization;

    private boolean edit;

    //跳转到添加界面
    @Action("/organization/apply")
    @Override
    public String execute() throws Exception {
        return "apply";
    }

    //添加、修改
    @Action("/organization/create")
    public String create() throws Exception {
        if(organization != null){
            Long id = organization.getId();
            if(organization.getParent() != null && organization.getParent().getId() != null && organization.getParent().getId() <= 0){
                organization.setParent(null);
            }

            if(id == null || id == 0){//create
                //保存的是顶级层次
                if(organization.getParent() == null || (organization.getParent() != null && organization.getParent().getId() == null)){
                    organization.setParent(null);
                    organization.setTopOrganization(null);
                }else{
                    Long parentId = organization.getParent().getId();
                    Organization parent = organizationService.get(parentId);
                    if(parent.getParent() == null){
                        organization.setTopOrganization(parent);
                    }else{
                        organization.setTopOrganization(parent.getTopOrganization());
                    }
                }
                organization.setStatus(AuditStatus.wait_audit);;
                organizationService.save(organization);
            }else{//edit
                Organization o = organizationService.get(id);
                if(o != null){
                    o.setParent(o.getParent() == null ? null : organization.getParent());
                    o.setFullName(organization.getFullName());
                    o.setAcronym(organization.getAcronym());
                    o.setLevel(organization.getLevel());
                    o.setEmployeeNo(organization.getEmployeeNo());
                    o.setEmployeeCount(organization.getEmployeeCount());

                    organizationService.save(o);
                }
            }
        }
        return "apply";
    }

    @Action("/organization/modify")
    public String modify() throws Exception {
        return create();
    }

   //跳转到修改界面
    @Action("/organization/edit")
    public String edit() throws Exception {
        edit = true;
        
        return "apply";
    }


    //组织架构数据写到考勤数据库中去start=============================================================================
    //初始化考勤系统部门数据库
    @Action(value = "/public/department-init", results = {@Result(name = "success", location = "department_index.jsp")})
    public String department_init() throws Exception {
        organizationService.initDepartmentDB();

        return "department-init";
    }
   //组织架构数据写到考勤数据库中去end=============================================================================

    public Organization getOrganization() {
        return organization;
    }

    public void setOrganization(Organization organization) {
        this.organization = organization;
    }


    public boolean isEdit() {
        return edit;
    }

    public void setEdit(boolean edit) {
        this.edit = edit;
    }
}
